﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Canteen.Core.Models.Migrations
{
    public partial class InitDb : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Stores",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CreateTime = table.Column<DateTime>(type: "DATETIME", nullable: false, defaultValueSql: "GETDATE()"),
                    UpdateTime = table.Column<DateTime>(type: "DATETIME", nullable: true),
                    Name = table.Column<string>(maxLength: 32, nullable: true),
                    Address = table.Column<string>(maxLength: 64, nullable: true),
                    Leader = table.Column<string>(maxLength: 8, nullable: true),
                    Remarks = table.Column<string>(maxLength: 256, nullable: true),
                    StoreImgUrl = table.Column<string>(maxLength: 256, nullable: true),
                    OpeningDate = table.Column<DateTime>(type: "DATE", nullable: true),
                    Phone = table.Column<string>(maxLength: 32, nullable: true),
                    PassWord = table.Column<string>(maxLength: 32, nullable: true),
                    Verify = table.Column<int>(nullable: false),
                    Status = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Stores", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Users",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CreateTime = table.Column<DateTime>(type: "DATETIME", nullable: false, defaultValueSql: "GETDATE()"),
                    UpdateTime = table.Column<DateTime>(type: "DATETIME", nullable: true),
                    Code = table.Column<string>(type: "VARCHAR(12)", maxLength: 12, nullable: false),
                    Name = table.Column<string>(maxLength: 32, nullable: true),
                    Sex = table.Column<int>(nullable: true),
                    IdCard = table.Column<string>(maxLength: 64, nullable: true),
                    HeadImgUrl = table.Column<string>(maxLength: 256, nullable: true),
                    Phone = table.Column<string>(maxLength: 32, nullable: true),
                    Email = table.Column<string>(maxLength: 256, nullable: true),
                    Account = table.Column<string>(maxLength: 256, nullable: true),
                    PassWord = table.Column<string>(maxLength: 32, nullable: true),
                    WeChatUnionid = table.Column<string>(maxLength: 28, nullable: true),
                    Logins = table.Column<int>(nullable: false),
                    Wallet = table.Column<decimal>(type: "MONEY", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Users", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Products",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CreateTime = table.Column<DateTime>(type: "DATETIME", nullable: false, defaultValueSql: "GETDATE()"),
                    UpdateTime = table.Column<DateTime>(type: "DATETIME", nullable: true),
                    Code = table.Column<string>(type: "VARCHAR(12)", maxLength: 12, nullable: false),
                    StoreId = table.Column<int>(nullable: false),
                    Name = table.Column<string>(maxLength: 64, nullable: true),
                    Type = table.Column<int>(nullable: true),
                    ProductImgUrl = table.Column<string>(maxLength: 256, nullable: true),
                    Price = table.Column<decimal>(type: "MONEY", nullable: true),
                    Number = table.Column<int>(nullable: false),
                    VitaminA = table.Column<decimal>(type: "DECIMAL(5,2)", nullable: true),
                    VitaminB = table.Column<decimal>(type: "DECIMAL(5,2)", nullable: true),
                    VitaminC = table.Column<decimal>(type: "DECIMAL(5,2)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Products", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Products_Stores_StoreId",
                        column: x => x.StoreId,
                        principalTable: "Stores",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "RechargeRecords",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CreateTime = table.Column<DateTime>(type: "DATETIME", nullable: false, defaultValueSql: "GETDATE()"),
                    UpdateTime = table.Column<DateTime>(type: "DATETIME", nullable: true),
                    UserId = table.Column<int>(nullable: false),
                    RechargeNo = table.Column<string>(type: "VARCHAR(12)", maxLength: 32, nullable: false),
                    Mode = table.Column<string>(maxLength: 16, nullable: true),
                    Price = table.Column<decimal>(type: "MONEY", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_RechargeRecords", x => x.Id);
                    table.ForeignKey(
                        name: "FK_RechargeRecords_Users_UserId",
                        column: x => x.UserId,
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "ShopOrders",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CreateTime = table.Column<DateTime>(type: "DATETIME", nullable: false, defaultValueSql: "GETDATE()"),
                    UpdateTime = table.Column<DateTime>(type: "DATETIME", nullable: true),
                    OrderNo = table.Column<string>(type: "VARCHAR(12)", maxLength: 32, nullable: false),
                    UserId = table.Column<int>(nullable: false),
                    StoreId = table.Column<int>(nullable: false),
                    Status = table.Column<int>(nullable: true),
                    PayStatus = table.Column<int>(nullable: false),
                    PayMode = table.Column<string>(maxLength: 16, nullable: true),
                    PayTime = table.Column<DateTime>(type: "DATETIME", nullable: true),
                    FinishTime = table.Column<DateTime>(type: "DATETIME", nullable: true),
                    Price = table.Column<decimal>(type: "MONEY", nullable: true),
                    Discount = table.Column<decimal>(type: "MONEY", nullable: true),
                    TotalPrice = table.Column<decimal>(type: "MONEY", nullable: true),
                    IsDel = table.Column<bool>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ShopOrders", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ShopOrders_Stores_StoreId",
                        column: x => x.StoreId,
                        principalTable: "Stores",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ShopOrders_Users_UserId",
                        column: x => x.UserId,
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "ShopOrderProducts",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CreateTime = table.Column<DateTime>(type: "DATETIME", nullable: false, defaultValueSql: "GETDATE()"),
                    UpdateTime = table.Column<DateTime>(type: "DATETIME", nullable: true),
                    ShopOrderId = table.Column<int>(nullable: false),
                    ProductId = table.Column<int>(nullable: false),
                    Number = table.Column<int>(nullable: false),
                    Price = table.Column<decimal>(type: "MONEY", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ShopOrderProducts", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ShopOrderProducts_Products_ProductId",
                        column: x => x.ProductId,
                        principalTable: "Products",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ShopOrderProducts_ShopOrders_ShopOrderId",
                        column: x => x.ShopOrderId,
                        principalTable: "ShopOrders",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Products_StoreId",
                table: "Products",
                column: "StoreId");

            migrationBuilder.CreateIndex(
                name: "IX_RechargeRecords_UserId",
                table: "RechargeRecords",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_ShopOrderProducts_ProductId",
                table: "ShopOrderProducts",
                column: "ProductId");

            migrationBuilder.CreateIndex(
                name: "IX_ShopOrderProducts_ShopOrderId",
                table: "ShopOrderProducts",
                column: "ShopOrderId");

            migrationBuilder.CreateIndex(
                name: "IX_ShopOrders_StoreId",
                table: "ShopOrders",
                column: "StoreId");

            migrationBuilder.CreateIndex(
                name: "IX_ShopOrders_UserId",
                table: "ShopOrders",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_Users_Phone",
                table: "Users",
                column: "Phone",
                filter: "[Phone] IS NOT NULL");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "RechargeRecords");

            migrationBuilder.DropTable(
                name: "ShopOrderProducts");

            migrationBuilder.DropTable(
                name: "Products");

            migrationBuilder.DropTable(
                name: "ShopOrders");

            migrationBuilder.DropTable(
                name: "Stores");

            migrationBuilder.DropTable(
                name: "Users");
        }
    }
}
